package com.fernfx.xingtan.my.presenter;

import android.app.Activity;
import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseEntity;
import com.fernfx.xingtan.common.base.BaseView;
import com.fernfx.xingtan.common.network.IRequestCallback;
import com.fernfx.xingtan.common.network.entity.NetworkOKResult;
import com.fernfx.xingtan.my.contract.UpdatePNumberContract;
import com.fernfx.xingtan.my.model.UpdatePNumberModel;
import com.fernfx.xingtan.my.ui.AccountSafeActivity;
import com.fernfx.xingtan.utils.FastJsonUtil;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import java.util.Map;

import io.rong.eventbus.EventBus;

/**
 * Created by admin on 2018/8/15.
 */

public class UpdatePNumberPresenter<P extends UpdatePNumberContract.View> implements UpdatePNumberContract.Presenter {
    private P P;
    private UpdatePNumberContract.Model model;
    private String newPnumber;

    @Override
    public void init(BaseView view) {
        this.P = (P)view;
        this.model = new UpdatePNumberModel();
    }

    public void setNewPnumber(String newPnumber){
        this.newPnumber = newPnumber;
    }

    @Override
    public void request(Map<String, Object> requestArgsMap) {
        model.request(requestArgsMap, new IRequestCallback() {
            @Override
            public void onSuccessful(NetworkOKResult result) {
                String message = result.getMessage();
                if (TextUtils.isEmpty(message)) {
                    onNetworkError(result.getMessage(), 500);
                    onNetworkError(result.getMessage(), 500);
                    return;
                }
                //修改昵称成功了之后做的逻辑
                BaseEntity baseEntity = FastJsonUtil.fromBean(message, BaseEntity.class);
                if (OtherUtil.checkRequestStatus(baseEntity)) {
                    //发送evenetbus
                    Intent intent = new Intent();
                    intent.putExtra(AccountSafeActivity.UPDATE_USER_NAME_KEY,newPnumber);
                    P.getActivity().setResult(Activity.RESULT_OK,intent);
                    //提示并关闭页面
                    ToastUtil.showCentertoast(baseEntity.getMsg());
                    P.getActivity().finish();
                }else {
                    ToastUtil.showCentertoast(baseEntity.getMsg());
                }
            }

            @Override
            public void onFailure(String errorMessage, int errorCode) {
                if (TextUtils.isEmpty(errorMessage)) {
                    onNetworkError(errorMessage, errorCode);
                    return;
                }

                final BaseEntity baseEntity = FastJsonUtil.fromBean(errorMessage, BaseEntity.class);
                if (null != baseEntity && !TextUtils.isEmpty(baseEntity.getMsg())) {
                    ToastUtil.showCentertoast(baseEntity.getMsg());
                }
            }

            @Override
            public void onNetworkError(String errorMessage, int errorCode) {
                ToastUtil.showCentertoast(R.string.network_error);
            }
        });
    }

    @Override
    public void detachView() {
        if (null != model) {
            model = null;
        }
    }

    @Override
    public CountDownTimer getTimeCount(final EditText edit,final TextView clickTv) {
        edit.setFocusable(true);
        edit.setFocusableInTouchMode(true);
        edit.requestFocus();

        return new CountDownTimer(60000, 1000) {
            private boolean flag = true;

            @Override
            public void onTick(long millisUntilFinished) {
                if (flag) {
                    clickTv.setClickable(false);
                    flag = false;
                }
                clickTv.setText(millisUntilFinished / 1000 + "秒");
            }

            @Override
            public void onFinish() {
                clickTv.setText("重新发送");
                clickTv.setClickable(true);
            }
        };
    }
}
